using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using TapTap.Login;
using TapTap.Common;
using TapTap.Bootstrap;
using System.Threading.Tasks;
using UnityEngine.SceneManagement;

public class LoginPanel : MonoBehaviour
{
    public Button m_BtnTapTap;
    public Button m_BtnLogout;
    public Button m_BtnExist;
    public Button m_BtnVisitor;
    public Button m_BtnEnterGame;
    public GameObject m_LoginGo;
    public GameObject m_EnterGo;
    public ExistConfirmPanel m_ExistConfirmPanel;
    public AudioManager m_AudioManager;
    public LoginManager m_LoginManager;

    void Awake()
    {
        m_BtnTapTap.onClick.AddListener(OnBtnTapTapClick);
        m_BtnVisitor.onClick.AddListener(OnBtnVisitorClick);
        m_BtnEnterGame.onClick.AddListener(OnBtnEnterGameClick);
        m_BtnLogout.onClick.AddListener(OnBtnLogoutClick);
        m_BtnExist.onClick.AddListener(OnBtnExistClick);

        if (AudioManager.Instance == null)
        {
            GameObject.Instantiate<GameObject>(m_AudioManager.gameObject);
        }

        if (LoginManager.Instance == null)
        {
            GameObject.Instantiate<GameObject>(m_LoginManager.gameObject);
        }
    }

    void Start()
    {
        AudioManager.PlayBGMAudio();
        UpdateBtnType();
    }

    async void OnBtnVisitorClick()
    {
        bool loginSuccess = await LoginManager.Instance.LoginAnonymously();

        if (loginSuccess)
        {
            EnterGame();
        }
    }

    async void OnBtnTapTapClick()
    {
        bool loginSuccess = await LoginManager.Instance.LoginWithTapTap();
        if (loginSuccess)
        {
            EnterGame();
        }
    }

    void OnBtnEnterGameClick()
    {
        EnterGame();
    }

    void EnterGame()
    {
        SceneManager.LoadSceneAsync("SelectMode", LoadSceneMode.Single);
    }

    void OnBtnExistClick()
    {
        m_ExistConfirmPanel.Show();
    }

    async void OnBtnLogoutClick()
    {
        await LoginManager.Instance.Logout();
        m_LoginGo.SetActive(true);
        m_EnterGo.SetActive(false);
    }

    void Update()
    {
        if (Input.GetKeyUp(KeyCode.Escape))
        {
            OnNavigationBack();
        }
    }

    void OnNavigationBack()
    {
        if (m_ExistConfirmPanel.gameObject.activeSelf)
        {
            m_ExistConfirmPanel.Hide();
            return;
        }

        if (m_EnterGo.activeSelf)
        {
            OnBtnLogoutClick();
        }
        else
        {
            OnBtnExistClick();
        }
    }

    async void UpdateBtnType()
    {
        try
        {
            bool isLogin = await LoginManager.Instance.IsLogin();
            m_EnterGo.SetActive(isLogin == true);
            m_LoginGo.SetActive(isLogin == false);
        }
        catch (Exception e)
        {
            Debug.Log(e.Message);
            m_EnterGo.SetActive(true);
            m_LoginGo.SetActive(false);
        }
    }
}
